home *** CD-ROM | disk | FTP | other *** search
- #ifdef RCSID
- static char *RCSid =
- "$Header: examp9.pc.d,v 1.2.720.1 95/02/21 17:49:20 xxxxxxxx: Needtomrg_7_2 $ ";
- #endif /* RCSID */
-
- /* Copyright (c) 1991 by Oracle Corporation */
- /*
- NAME
- examp9.pc - <one-line expansion of the name>
- DESCRIPTION
- <short description of component this file declares/defines>
- PUBLIC FUNCTION(S)
- <list of external functions declared/defined - with one-line descriptions>
- PRIVATE FUNCTION(S)
- <list of static functions defined in .c file - with one-line descriptions>
- RETURNS
- <function return values, for .c file with single function>
- NOTES
- <other useful comments, qualifications, etc.>
- MODIFIED (MM/DD/YY)
- xxxxxxxx 02/13/95 - update
- xxxxxxxx 05/12/92 - Creation
- */
- /************************************************************************
- * *
- * EMBEDDED PL/SQL DEMO *
- * *
- * This program shows the use of host variables. It prompts for the *
- * name of an employee, then executes a PL/SQL block that uses four *
- * SELECT statements to get information about the employee. The *
- * information returned includes: job title, hire date, number of *
- * people who have served the company longer, salary, number of people *
- * who have a higher salary, department number, and number of people *
- * in that department. *
- * *
- * Copyright (c) 1989,1992 by Oracle Corporation. *
- ************************************************************************/
-
- #include <stdio.h>
-
- EXEC SQL BEGIN DECLARE SECTION;
- VARCHAR empname[11];
- VARCHAR jobtype[9];
- VARCHAR hired[9];
- int salary;
- int dept;
- int served_longer;
- int higher_sal;
- int total_in_dept;
- VARCHAR uid[20];
- VARCHAR pwd[20];
- EXEC SQL END DECLARE SECTION;
-
- EXEC SQL INCLUDE SQLCA;
-
- void sqlerror();
- main()
- {
- /* Set up userid and password */
- strcpy (uid.arr,"scott");
- uid.len = strlen(uid.arr);
- strcpy (pwd.arr,"tiger");
- pwd.len = strlen(pwd.arr);
-
- printf("\n\n\tEmbedded PL/SQL Demo\n\n");
- printf("Trying to connect...");
- /* Check for SQL errors */
- EXEC SQL WHENEVER SQLERROR DO sqlerror();
- /* Connect to Oracle */
- EXEC SQL CONNECT :uid IDENTIFIED BY :pwd;
- printf(" connected.\n");
-
- for (;;) /* Loop indefinitely */
- {
- printf("\n** Name of employee? (<CR> to quit) ");
- gets(empname.arr); /* Get the name */
- if (strlen(empname.arr) == 0) /* No name entered, */
- {
- EXEC SQL COMMIT WORK RELEASE; /* so log off Oracle */
- exit(0); /* and exit program */
- }
- empname.len = strlen(empname.arr);
- jobtype.len = 9;
- hired.len = 9;
-
- /* ----- Begin PL/SQL block ----- */
- EXEC SQL EXECUTE
- BEGIN
- SELECT job, hiredate, sal, deptno
- INTO :jobtype, :hired, :salary, :dept FROM emp
- WHERE ename = UPPER(:empname);
- /* Get number of people whose length *
- * of service is longer */
- SELECT COUNT(*) INTO :served_longer FROM emp
- WHERE hiredate < :hired;
- /* Get number of people with a higher salary */
- SELECT COUNT(*) INTO :higher_sal FROM emp
- WHERE sal > :salary;
- /* Get number of people in same department */
- SELECT COUNT(*) INTO :total_in_dept FROM emp
- WHERE deptno = :dept;
- END;
- END-EXEC;
- /* ----- End PL/SQL block ----- */
-
- /* Null-terminate character strings returned by Oracle */
- jobtype.arr[jobtype.len] = '\0';
- hired.arr[hired.len] = '\0';
- /* Display the information */
- printf("\n%s's job is: %s\n", empname.arr, jobtype.arr);
- printf("Hired on: %s\n", hired.arr);
- printf(" %d people have served longer\n", served_longer);
- printf("Salary is: %d\n", salary);
- printf(" %d people have a higher salary\n", higher_sal);
- printf("Department number is: %d\n", dept);
- printf(" %d people in the department\n", total_in_dept);
- } /* End of loop */
- }
-
- void sqlerror()
- {
- /* Avoid infinite loop if rollback causes an error */
- EXEC SQL WHENEVER SQLERROR CONTINUE;
- printf("\nOracle error detected:\n");
- /* Print error message and disconnect from Oracle */
- printf("\n%.70s\n", sqlca.sqlerrm.sqlerrmc);
- EXEC SQL ROLLBACK WORK RELEASE;
- exit(1);
- }
-
-